package model

import "github.com/jinzhu/gorm"

type User struct {
	*Model
	Name            string `json:"name"`
	Email           string `json:"email"`
	EmailVerifiedAt string `json:"email_verified_at"`
	Password        string `json:"password"`
	RememberToken   string `json:"remember_token"`
}

func (u User) TableName() string {
	return "users"
}

func (u User) Login(db *gorm.DB) (User, error) {
	var user User
	db = db.Where("email = ? and password = ?", u.Email, u.Password)
	err := db.First(&user).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return user, err
	}

	return user, nil
}

func (u User) Register(db *gorm.DB) (User, error) {
	err := db.Create(&u).Error
	if err != nil {
		return u, err
	}

	return u, nil
}
